import {
    setStore,
    getStore
  } from '@/util/store'
import {
    getAllRequesInfo,
    getAllUpsSuppliersInfo,
    getUserOfdiffrent,
    getProductsInfo
} from '@/api/common'
const dicData = {
    state: {
        upsSuppliersInfo: getStore({ //上游商信息
            name: 'upsSuppliersInfo'
        }) || [],
        downSuppliersInfo: getStore({ //下游商信息
            name: 'downSuppliersInfo'
        }) || [], 
        businessInfo: getStore({ //获取商务信息
            name: 'businessInfo'
        }) || [], 
        productsInfo: getStore({ //产品信息
            name: 'productsInfo'
        }) || [], 
        allRequesInfo: getStore({ //请求信息
            name: 'allRequesInfo'
        }) || [], 
    },
    actions: {
        GetAllUpsSuppliersInfo({
            commit,
            state,
            dispatch
        }){
            return new Promise((resolve, reject) => {
                getAllUpsSuppliersInfo().then(response => {
                    const data = response.data.data;
                    commit('SET_SUPPLIERS_INFO',data);
                    resolve(response);
                });
            }).catch( error => {
                reject(error);
            });
        },
        GetUserOfdiffrent({
            commit,
            state,
            dispatch
        },type){
            return new Promise((resolve, reject) => {
                getUserOfdiffrent({userType: type}).then(response => {
                    const data = response.data.data;
                    commit('SET_DOWN_SUPPLIERS_INFO',data);
                    resolve(response);
                });
            }).catch( error => {
                reject(error);
            }); 
        },
        GetBusinessInfo({
            commit,
            state,
            dispatch
        },type){
            return new Promise((resolve, reject) => {
                getUserOfdiffrent({userType: type}).then(response => {
                    const data = response.data.data;
                    commit('SET_BUSINESS_INFO',data);
                    resolve(response);
                });
            }).catch( error => {
                reject(error);
            }); 
        },
        GetProductsInfo({
            commit,
            state,
            dispatch 
        },type){
            return new Promise((resolve, reject) => {
                getProductsInfo().then(response => {
                    const data = response.data.data;
                    commit('SET_PRODUCTS_INFO',data);
                    resolve(response);
                });
            }).catch( error => {
                reject(error);
            }); 
        },
        GetAllRequesInfo({
            commit,
            state,
            dispatch 
        },type){
            return new Promise((resolve, reject) => {
                getAllRequesInfo().then(response => {
                    const data = response.data.data;
                    commit('SET_REQUERST_INFO',data);
                    resolve(response);
                });
            }).catch( error => {
                reject(error);
            }); 
        }
    },
    mutations: {
        SET_SUPPLIERS_INFO(state, data){
            let tempArr = [];
            data.forEach(item => {
                tempArr.push({
                    label: item.upsName,
                    value: item.id
                });
            });
            state.upsSuppliersInfo = tempArr;
            setStore({
                name: 'upsSuppliersInfo',
                content: tempArr
            });
        },
        SET_DOWN_SUPPLIERS_INFO(state, data){
            let tempArr = [];
            data.forEach(item => {
                tempArr.push({
                    label: item.agentName,
                    value: item.userId  
                });
            });
            state.downSuppliersInfo = tempArr;
            setStore({
                name: 'downSuppliersInfo',
                content: tempArr
            });
        },
        SET_BUSINESS_INFO(state, data){
            let tempArr = [];
            data.forEach(item => {
                tempArr.push({
                    label: item.agentName,
                    value: item.userId  
                });
            });
            state.businessInfo = tempArr;
            setStore({
                name: 'businessInfo',
                content: tempArr
            });
        },
        SET_PRODUCTS_INFO(state, data){
            let tempArr = [];
            data.forEach(item => {
                tempArr.push({
                    label: item.name,
                    value: item.id,
                    upsName: item.upsName,
                    businessName: item.businessName
                });
            });
            state.productsInfo = tempArr;
            setStore({
                name: 'productsInfo',
                content: tempArr
            });
        },
        SET_REQUERST_INFO(state, data){
            let tempArr = [];
            data.forEach(item => {
                tempArr.push({
                    label: item.requestName,
                    value: item.id
                });
            });
            state.allRequesInfo = tempArr;
            setStore({
                name: 'allRequesInfo',
                content: tempArr
            });
        }
    }
}
export default dicData